const App = getApp();
const navigationBarHeight = (getApp().diygwstatusBarHeight + 44) + 'px'
Page({
    data: {
        date:"",
        date2: "",
        msgs1: [],
        msgs2: [],
        ymoney:0,
        zmoney:0,
        id:0,
        cid:0,
        price:0,
        url: App.Config.basePath,
        buttons3: [{
            type: 'submit',
            url: App.renderUrl('empty', 'index'),
            title: ``
        }]
    },

   //弹出提示信息
    showModal(message) {
        App.WxService.showModal({
            title: '友情提示',
            content: message,
            showCancel: !1,
        });
    },
    onShareAppMessage: function() {},

    //页面加载
    onLoad(option) {
        if (option) {
            this.setData({
                globalOption: option,
                date:option.d1,
                date2:option.d2
            })
        }
    },

   //页面显示
    onShow() {
        this.getForm1();
        this.chushi();
        this.getMsgs1();
    },

    //设置输入验证
    getForm1() {
        this.WxValidate = App.WxValidate({


mname: {
		required:{
			message:'姓名：不能为空哟'
		}
	},
tel: {
		tel:{
			message:'手机号码：请输入11位的手机号码'
		},
		required:{
			message:'手机号码：不能为空哟'
		}
	},
cardno: {
		required:{
			message:'身份证号：不能为空哟'
		}
	},

        })
    },

    //提交
    formSubmit1: function(e) {
        if (!this.WxValidate.checkForm(e)) {  //输入验证不通过时，弹出相应的提示信息，并返回
            const error = this.WxValidate.errorList[0]
            var msg = `${error.msg}`;
            this.showModal(msg);
            return false
        }
        else if(this.data.zmoney==0)
        {
            this.showModal("请选择要预定的房间编号")
        }
        else {
            var data = e.detail.value;

            data = App.Tools.extend(data, {
                mid: wx.getStorageSync("mid"),
                rid: this.data.globalOption.id,
                d1:this.data.date,
                d2:this.data.date2,
                m1:this.data.zmoney,
                m2:this.data.ymoney,
                id:this.data.id,
                room:JSON.stringify(this.data.msgs1),
            });

            App.HttpService.saveData(data, "/orders_Add").then(data => { //执行服务器端Servlet，并根据返回值，弹出相应的提示
                App.WxService.showToast({
                    title: "预定成功!",
                    icon: 'success',
                    duration: 1500,
                });
                setTimeout(function() {
                    wx.switchTab({
                        url:"/pages/order/index"
                    })
                }, 1500)
            });
        }
    },
    formReset1: function() {
        console.log('form发生了reset事件')
    },

    //初始化用户信息
    chushi() {
        //设置要传递的参数
        let param = {
            f: 1,
            mid: wx.getStorageSync("mid"),
            loadmsg: `正在加载中`
        };


        App.HttpService.getData(param, "/members_List").then(data => { //执行服务器Servlet

            this.setData({

                users: data  //把从服务器端得到的值赋值给数组
            });
        });
    },

    bindDateChange: function(e) {



        this.setData({
            date: e.detail.value
        })
        this.jisuan();
    },

    bindDateChange2: function(e) {



        this.setData({
            date2: e.detail.value
        })
        this.jisuan();
    },


    getMsgs1() {

        //设置要传递的参数
        let param = {
            f: 1,
            rid: this.data.globalOption.id,
            d1:this.data.date,
            d2:this.data.date2,
            loadmsg: `正在加载中`
        };

        App.HttpService.getData(param, "/rooms_List").then(data => { //执行服务器Servlet
            this.setData({
                msgs1: data //把从服务器端得到的值赋值给数组
            });
        });
    },

    xuanzi(e)
    {
        var ss=this.data.msgs1;
        ss[e.currentTarget.dataset.index].flag2=ss[e.currentTarget.dataset.index].flag2==1?0:1;

        this.setData({
            msgs1:ss
        })

        this.jisuan();
    },

    jisuan()
    {
        var endTime = new Date(this.data.date2).getTime() / 1000 - parseInt(new Date(this.data.date).getTime() / 1000);
        var timeDay = parseInt(endTime / 60 / 60 / 24);        //相差天数

        var quan=0;
        var ss=this.data.msgs1;
        for (var i=0;i<ss.length;i++)
        {
            if(ss[i].flag2==1)
            {
                quan++;
            }
        }

        var t1=parseFloat(this.data.globalOption.p)*parseInt(timeDay)*parseInt(quan);
        this.setData({
            zmoney:t1,
            ymoney:t1==0?0:t1-this.data.price,
        })

        let param = {
            f: 1,
            mid: wx.getStorageSync("mid"),
            t:t1,
            loadmsg: `正在加载中`
        };

        App.HttpService.getData(param, "/receives_List").then(data => { //执行服务器Servlet
            this.setData({
                msgs2: data //把从服务器端得到的值赋值给数组
            });
        });

    },

    lingqu(e)
    {
        this.setData({
            id:e.currentTarget.dataset.id,
            cid:e.currentTarget.dataset.cid,
            price:e.currentTarget.dataset.q,
            ymoney:parseFloat(this.data.zmoney)-e.currentTarget.dataset.q
        })
    }


})
